/**
* @Title: integrityOrgDialog_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年11月3日 下午9:21:08
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('IntegrityOrgDialogCtrl', ['$scope', '$http','BaseURL', 'Modal','$modalInstance','IntegrityMedicalService', function($scope, $http, baseUrl, Modal,$modalInstance,IntegrityMedicalService){
		$scope.integrityMedicalService = IntegrityMedicalService;
		$scope.queryMedical = function (){
			IntegrityMedicalService.getIntegrityOrg({orgName:$scope.form.name}).success(function(res){
				IntegrityMedicalService.systemIntegrityMarks=res;
			});
		}
		
		//初始化搜索框值
		$scope.form.name="";
		if(IntegrityMedicalService.ownerIntegrityMarks){
			angular.forEach(IntegrityMedicalService.ownerIntegrityMarks,function(data,index){
				angular.forEach(IntegrityMedicalService.systemIntegrityMarks,function(integrity,index){
					var isflat = (data.code == integrity.code);
					if(data.code === integrity.code){
						integrity.selected = integrity.selected == true?false:true;
					}
				});
			});
		}
		
		$scope.selectUserInfoList= [];
		//关闭model
		$scope.onClose = function() {
			var owner = [];
			owner = IntegrityMedicalService.ownerIntegrityMarks;
			IntegrityMedicalService.ownerIntegrityMarks = [];
			$modalInstance.close(owner);
		}
		
		function add(src, tar) {
			var selected = [], unselected = [];
			$.each(src, function(){
				if(this.selected) {
					selected.push(this);
					this.selected = false;
				} else {
					unselected.push(this);
				}
			})
			return [src, tar.concat(selected)];
		}
		 $scope.add=function(){
	        	var arrs = add(IntegrityMedicalService.systemIntegrityMarks, IntegrityMedicalService.ownerIntegrityMarks);
	        	IntegrityMedicalService.systemIntegrityMarks = arrs[0];
	        	IntegrityMedicalService.ownerIntegrityMarks = arrs[1];
			}
        //查询过滤
	      $scope.searchFilter = function(data) {
	    	  if(angular.isUndefined($scope.form.name) == true || $scope.form.name == ''){
	    		  return true;
	    	  }
	    	  if(data != null && data.name && data.name.indexOf($scope.form.name) != -1) {
	    		  return true;
	    	  }
	    	  return false;
	      }
	      
	      //确定按钮事件
			$scope.selectMedicalOrg = function() {
				IntegrityMedicalService.ownerIntegrityMarks = [];
				$scope.add();
				$modalInstance.close(IntegrityMedicalService.ownerIntegrityMarks);
			 }
			
		$modalInstance.result.then(function (selectedItem) {
			    	//确定
			    	if(selectedItem){
			    	 var selectDataList=new Array();
			    	 var codeArrs=new Array();
			    	 $.each(selectedItem,function(index,item){
			    		 selectDataList.push(item.select);
			    		 codeArrs.push(item.code);
			    	 });
			    	 $scope.form.orgCode = "";
			    	 $scope.form.orgName="";
			    	 $scope.form.orgCode=codeArrs.toString();
			    	 $scope.form.orgName=selectDataList.toString();
			    	}
			    });
		$scope.onIntegrityItemClick = function(integrity) {
			integrity.selected = integrity.selected == true?false:true;
		}
	}]);
})();